import React from 'react';
import { View, Platform } from 'react-native';
import RootSiblings from 'react-native-root-siblings';
import SpinKit from 'react-native-spinkit';
import { DefaultStyles } from '~/styles/GlobalStyles';
import { adapt } from '~/utils/ScreenAdapter';

let elements = [];
export default class Loader {
  static show = (backgroundColor, size, color) => {
    size = size || 50;
    color = color || DefaultStyles.ThemeColor;
    backgroundColor = backgroundColor || DefaultStyles.Black32;

    let sibling = new RootSiblings(
      (
        <View
          style={{
            position: 'absolute',
            top: Platform.OS === 'android' ? 0 : 0,
            right: 0,
            bottom: 0,
            left: 0,
            width: adapt(375),
            height: '100%',
            backgroundColor: backgroundColor,
            justifyContent: 'center',
            alignItems: 'center',
          }}
        >
          <SpinKit isVisible={true} size={size} type={'Bounce'} color={color} />
        </View>
      )
    );
    elements.push(sibling);
  };

  static hide = () => {
    let lastSibling = elements.pop();
    lastSibling && lastSibling.destroy();
  };
}
